package com.piggy.oss.utils;

import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson2.JSON;
import com.aliyun.ocr20191230.Client;
import com.aliyun.ocr20191230.models.RecognizePdfRequest;
import com.aliyun.ocr20191230.models.RecognizePdfResponse;
import com.aliyun.teaopenapi.models.Config;
import com.piggy.common.core.constant.Constants;
import com.piggy.common.core.exception.base.BaseException;
import com.piggy.common.core.utils.SpringUtils;
import com.piggy.common.redis.service.RedisService;
import com.piggy.oss.cloud.CloudStorageConfig;
import com.piggy.oss.cloud.OSSFactory;
import com.piggy.oss.config.OCRConfig;
import com.piggy.oss.constants.CloudConstants;
import org.springframework.stereotype.Component;

public class OCRUtils {
	private static RedisService redisCache;

	static {
		OCRUtils.redisCache =  SpringUtils.getBean(RedisService.class);
	}
	/**
	 * 使用AK&SK初始化阿里云账号Client
	 * @param accessKeyId
	 * @param accessKeySecret
	 * @return Client
	 * @throws Exception
	 */
	public static Client createClient(String accessKeyId, String accessKeySecret) throws Exception {
		Config config = new Config()
			// 您的AccessKey ID
			.setAccessKeyId(accessKeyId)
			// 您的AccessKey Secret
			.setAccessKeySecret(accessKeySecret);
		// 访问的域名
		config.endpoint = "ocr.cn-shanghai.aliyuncs.com";
		return new Client(config);
	}

	/**
	 * 调用阿里云视觉平台识别
	 * @param url
	 * @return
	 * @throws Exception
	 */
	public static String pdfOcr(String url) {
		StringBuffer result=new StringBuffer();
		try{
			//获取云存储配置信息
			String cloudConfig = redisCache.getCacheObject(Constants.SYS_CONFIG_KEY + CloudConstants.CONTRACT_CLOUDCONFIG);
			if (StrUtil.isEmpty(cloudConfig))throw new BaseException("系统未配置云服务");
			CloudStorageConfig config = JSON.parseObject(cloudConfig, CloudStorageConfig.class);

			Client client = createClient(config.getAliyunAccessKeyId(), config.getAliyunAccessKeySecret());
			RecognizePdfRequest recognizePdfRequest = new RecognizePdfRequest()
				.setFileURL(url);
			// 复制代码运行请自行打印 API 的返回值
			RecognizePdfResponse recognizePdfResponse = client.recognizePdf(recognizePdfRequest);
//        System.out.println(recognizePdfResponse.getBody().getData().getWordsInfo());

			for(int i=0;i<recognizePdfResponse.body.getData().getWordsInfo().size();i++){
				result.append(recognizePdfResponse.body.getData().getWordsInfo().get(i).getWord().replaceAll(" ",""));
			}

		}catch (Exception e){
//			throw new BaseException("网络异常，请重新上传");
			result.append("OCR mistake!");
		}
		return result.toString();

	}
	public  String pdfOcr1(String url) throws Exception {
		//获取云存储配置信息
		String cloudConfig = redisCache.getCacheObject(Constants.SYS_CONFIG_KEY + CloudConstants.CONTRACT_CLOUDCONFIG);
		if (StrUtil.isEmpty(cloudConfig))throw new BaseException("系统未配置云服务");
		CloudStorageConfig config = JSON.parseObject(cloudConfig, CloudStorageConfig.class);

		Client client = createClient(config.getAliyunAccessKeyId(), config.getAliyunAccessKeySecret());
		RecognizePdfRequest recognizePdfRequest = new RecognizePdfRequest()
			.setFileURL(url);
		// 复制代码运行请自行打印 API 的返回值
		RecognizePdfResponse recognizePdfResponse = client.recognizePdf(recognizePdfRequest);
//        System.out.println(recognizePdfResponse.getBody().getData().getWordsInfo());
		StringBuffer result=new StringBuffer();
		for(int i=0;i<recognizePdfResponse.body.getData().getWordsInfo().size();i++){
			result.append(recognizePdfResponse.body.getData().getWordsInfo().get(i).getWord().replaceAll(" ",""));
		}
		return result.toString();
	}

	public static void main(String[] args_) throws Exception {
//		String pdfFilePath = "C:\\Users\\Administrator\\Pictures\\平行车业务相关单据\\2.招商船企181仓验车单 _363169(整改前).pdf";
//		String conStr = "W1N4633491X363169";

//		String s = pdfOcr("https://edu-210704.oss-cn-shanghai.aliyuncs.com/LCpdf.pdf?versionId=CAEQFhiBgICs0qWK1hciIDk3OWJiMzFkZmM1NjRjOWZiYzNiMWY4ZWI0OTZkYzAz");
//		System.out.println(s);
//		System.out.println(s.contains("YND20200923"));


//		java.util.List<String> args = java.util.Arrays.asList(args_);
//		com.aliyun.ocr20191230.Client client = createClient("LTAI4GGkchuseJgZqNG892Si", "RNy8GcOfUJXBhUBqwy3ljZ8ckrUF3q");
//		RecognizePdfRequest recognizePdfRequest = new RecognizePdfRequest()
//			.setFileURL("https://edu-210704.oss-cn-shanghai.aliyuncs.com/BL.pdf?versionId=CAEQFhiBgMDAjcqQ1RciIGUyZjYzNjc3YWIzZTQ4MjA5ZTE2NGQzNDE1NzAxYzA0");
//		// 复制代码运行请自行打印 API 的返回值
//		RecognizePdfResponse recognizePdfResponse = client.recognizePdf(recognizePdfRequest);
////        System.out.println(recognizePdfResponse.getBody().getData().getWordsInfo());
//		StringBuffer result=new StringBuffer();
//		for(int i=0;i<recognizePdfResponse.body.getData().getWordsInfo().size();i++){
//			result.append(recognizePdfResponse.body.getData().getWordsInfo().get(i).getWord().replaceAll(" ",""));
//		}
//		System.out.println(result);
//		System.out.println(result.toString().contains("W09GD3056LOM92509"));

	}
}
